package	com.mes.service.llm;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mes.entity.check.PatrolCheck;
import com.mes.entity.check.ReportCheck;
import com.mes.entity.check.TreeCheck;
import com.mes.entity.llm.ChartLlm;
import com.mes.entity.llm.FillRecord;
import com.mes.entity.llm.SaleDeliveryReport;
import com.mes.service.llm.ChartLlmService;
import com.mes.utils.DateUtil;
import com.mes.dao.llm.ChartLlmMapper;
/**
 *
 *@ClassName: ChartService
 *@Description: 报表金额
 *@Author llm
 *@Date 2019-05-31 14:10:08
 *@version 1.0
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class ChartLlmServiceImpl implements ChartLlmService {
	@Resource ChartLlmMapper ChartLlmMapper;
	/** 
	 * 根据主键id查询实体
	 * @param id 主键id
	 * @return 实体
	*/
	public ChartLlm queryById(ChartLlm chartLlm) {
		return ChartLlmMapper.queryById(chartLlm);
	}
	/** 
	 * 条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	public List<ChartLlm> queryAllByMution(ChartLlm param) {
		return ChartLlmMapper.queryAllByMution(param);
	}
	/** 
	 * 条件分页查询
	 * @param param 实体条件
	 * @return 实体集合
	*/
	public List<ChartLlm> findPageByMution(ChartLlm param) {
		return ChartLlmMapper.findPageByMution(param);
	}
	/** 
	 * 条件查询记录数
	 * @param param 实体条件
	 * @return 实体集合
	*/
	public Integer selectCountByMution(ChartLlm param) {
		return ChartLlmMapper.selectCountByMution(param);
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @return 主键id
	*/
	public Long insert(ChartLlm param) {
		return ChartLlmMapper.insert(param);
	}
	/** 
	 * 根据主键更新 返回影响行数
	 * @param param 实体条件
	 * @return 影响行数
	*/
	public Integer update(ChartLlm param) {
		return ChartLlmMapper.update(param);
	}
	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param ids 主键拼接的字符串
	 * @return 影响行数
	*/
	public Integer delete(String ids) {
		return ChartLlmMapper.delete(ids);
	}
	/**      
	 * 根据科目id查询实体 
	 * @param cid
	 * @return    
	 */
	@Override
	public ChartLlm queryByCid(ChartLlm chartLlm) {
		return ChartLlmMapper.queryByCid(chartLlm);
	}
	/**     
	 * 销售订单发货统计表    
	 * @param map
	 * @return    
	 */
	@Override
	public List<SaleDeliveryReport> selectSaleDelivery(Map<String, Object> map) {
		return ChartLlmMapper.selectSaleDelivery(map);
	}
	/**      
	 * 订单准时发货率统计表  
	 * @param map
	 * @return    
	 */
	@Override
	public List<SaleDeliveryReport> selectPunctualitySale(Map<String, Object> map) {
		List<SaleDeliveryReport> list = ChartLlmMapper.selectPunctualitySale(map);
		for(SaleDeliveryReport sdr : list){
			map.put("productId", sdr.getProductId());
			map.put("orderDetailId", sdr.getDetailId());
			//根据订单详情ID，产品ID，查询准时出库数量（即出库时间<订单计划时间）, 累计出库数量
			SaleDeliveryReport temp = ChartLlmMapper.selectOutQuantity(map);
			//根据订单详情ID，产品ID，查询最后一次的出库时间
			SaleDeliveryReport temp2 = ChartLlmMapper.selectRecentlyDeliverDate(map);
			//计算准时发货率（准时出库数量/累计出库数量）
			if(temp != null){
				sdr.setOutQuantity(temp.getOutQuantity());
				sdr.setPanctalityOutQuantity(temp.getPanctalityOutQuantity());
				Integer panctalityOutQuantity = temp.getPanctalityOutQuantity();
				Integer planQuantity = sdr.getPlanQuantity();
				DecimalFormat df=new DecimalFormat("0.00%");//设置保留位数
				sdr.setPunctality(df.format((double)panctalityOutQuantity/planQuantity));
				df = null;
				panctalityOutQuantity = null;
				planQuantity = null;
			}else{
				sdr.setPunctality("0.00%");
			}
			if(temp2 != null){
				sdr.setRecentlyDeliverDate(temp2.getRecentlyDeliverDate());
			}
			temp = null;
			temp2 = null;
		}
		return list;
	}
	/**    
	 * 客户返品比率统计表    
	 * @param map
	 * @return    
	 */
	@Override
	public List<SaleDeliveryReport> selectReturnRatil(Map<String, Object> map) {
		List<SaleDeliveryReport> list = ChartLlmMapper.selectReturnRatil(map);
		for(SaleDeliveryReport sdr : list){
			if(sdr != null){
				Integer outQuantity = sdr.getOutQuantity();
				Integer returnQuantity = sdr.getReturnQuantity();
				if(outQuantity == 0 || outQuantity == null || returnQuantity == 0 || returnQuantity == null){
					sdr.setPunctality("0.00%");
				}else{
					DecimalFormat df=new DecimalFormat("0.00%");//设置保留位数
					sdr.setPunctality(df.format((double)returnQuantity/outQuantity));
					df = null;
				}
				outQuantity = null;
				returnQuantity = null;
			}
		}
		return list;
	}
	/**     
	 * 完成品库存日报表  
	 * @param map
	 * @return    
	 */
	@Override
	public List<SaleDeliveryReport> selectProductStockByDay(Map<String, Object> map) {
		List<SaleDeliveryReport> list = ChartLlmMapper.selectProductStockByDay(map);
		if(list.size() > 0){
			//获取上一天的日期
			String str = DateUtil.getLastDay((String)map.get("dateTime"));
		    map.put("dateTime", str);
			for(SaleDeliveryReport sdr : list){
				if(sdr != null){
					map.put("productId", sdr.getProductId());
					SaleDeliveryReport temp = ChartLlmMapper.selectProductStockByLastDay(map);
					if(temp != null){
						sdr.setLastComeQuantity(temp.getComeQuantity());
						sdr.setLastOutQuantity(temp.getGoQuantity());
						sdr.setLastStockQuantity(temp.getStockQuantity());
					}
					temp = null;
				}
			}
		}
		return list;
	}
	/**  
	 * 资材库存日报表     
	 * @param map
	 * @return    
	 */
	@Override
	public List<SaleDeliveryReport> selectMaterielStockByDay(Map<String, Object> map) {
		return ChartLlmMapper.selectMaterielStockByDay(map);
	}
	/**    
	 * 查询质检部门的所有员工权限   
	 * @return    
	 */
	@Override
	public List<FillRecord> selectMenuByDepartment() {
		return ChartLlmMapper.selectMenuByDepartment();
	}
	/**   
	 * 查询初中终检填报记录      
	 * @param map
	 * @return    
	 */
	@Override
	public List<TreeCheck> selectTreeCheckFillRecord(Map<String, Object> map) {
		return ChartLlmMapper.selectTreeCheckFillRecord(map);
	}
	/** 
	 * 查询报检填报记录      
	 * @param map
	 * @return    
	 */
	@Override
	public List<ReportCheck> selectReportCheckFillRecord(Map<String, Object> map) {
		return ChartLlmMapper.selectReportCheckFillRecord(map);
	}
	/** 
	 * 查询巡检填报记录      
	 * @param map
	 * @return    
	 */
	@Override
	public List<PatrolCheck> selectPatrolCheckFillRecord(Map<String, Object> map) {
		return ChartLlmMapper.selectPatrolCheckFillRecord(map);
	}
}
